#include "windowskin.h"
#include <QPainter>

WindowSkin::WindowSkin(QObject* parent)
    :QObject(parent)
{
}




QPixmap WindowSkin::createWindow3Frame(int w, int h, int base, GSWindowStyle type){
    QPixmap result(w, h);
    result.fill(Qt::transparent);
    QPixmap styleFile =  WindowSkin::getStyleFile(type);
    QPixmap leftTop = styleFile.copy(base,0, 24, 24);
    QPixmap top = styleFile.copy(base+24, 0, 16, 16);
    QPixmap rightTop = styleFile.copy(base+40, 0, 24, 24);
    QPixmap left = styleFile.copy(base, 24, 16, 16);
    QPixmap right = styleFile.copy(base+48, 24, 16, 16);
    QPixmap leftBottom = styleFile.copy(base,40, 24, 24);
    QPixmap bottom = styleFile.copy(base+24, 48, 16, 16);
    QPixmap rightBottom = styleFile.copy(base+40, 40, 24, 24);


    QPainter p;
    p.begin(&result);
    for(int i = 0; leftTop.width()+top.width()*i<w-rightTop.width(); i++){
        if(leftTop.width()+top.width()*(i+1)<w-rightTop.width())
            p.drawPixmap(leftTop.width()+top.width()*i, 0, top.width(), top.height(), top);
        else
            p.drawPixmap(leftTop.width()+top.width()*i, 0,
                         w-leftTop.width()-rightTop.width()-top.width()*i, top.height(), top);
    }
    for(int i = 0; leftBottom.width()+bottom.width()*i<w-rightBottom.width(); i++){
        if(leftBottom.width()+bottom.width()*(i+1)<w-rightBottom.width())
            p.drawPixmap(leftBottom.width()+bottom.width()*i, h-bottom.height(), bottom.width(), bottom.height(), bottom);
        else
            p.drawPixmap(leftBottom.width()+bottom.width()*i, h-bottom.height(),
                         w-leftBottom.width()-rightBottom.width()-bottom.width()*i, bottom.height(), bottom);
    }

    for(int i = 0; leftTop.height()+left.height()*i<h-leftBottom.height(); i++){
        if(leftTop.height()+left.height()*(i+1)<h-leftBottom.height())
            p.drawPixmap(0, leftTop.height()+left.height()*i, left.width(), left.height(), left);
        else
            p.drawPixmap(0, leftTop.height()+left.height()*i, left.width(),
                         h-leftTop.height()-left.height()*i-leftBottom.height(), left);
    }

    for(int i = 0; rightTop.height()+right.height()*i<h-rightBottom.height(); i++){
        if(rightTop.height()+right.height()*(i+1)<h-rightBottom.height())
            p.drawPixmap(w-right.width(), rightTop.height()+right.height()*i, right.width(), right.height(), right);
        else
            p.drawPixmap(w-right.width(), rightTop.height()+right.height()*i, right.width(),
                         h-rightTop.height()-right.height()*i-leftBottom.height(), right);
    }
    p.drawPixmap(w-right.width(), h-rightBottom.height()-right.height(), right.width(), right.height(), right);


    p.drawPixmap(0,0, leftTop.width(), leftTop.height(), leftTop);
    p.drawPixmap(w-rightTop.width(), 0, rightTop.width(), rightTop.height(), rightTop);
    p.drawPixmap(0, h-leftBottom.height(), leftBottom.width(), leftBottom.height(), leftBottom);
    p.drawPixmap(w-rightBottom.width(), h-rightBottom.height(), rightBottom.width(), rightBottom.height(), rightBottom);
    p.end();
    return result;
}

QPixmap WindowSkin::createWindowFrame(int w, int h, GSWindowStyle type){
    if(type<10)
        return WindowSkin::createWindow3Frame(w, h, 128, type);
    else
        return WindowSkin::createWindow3Frame(w, h, 64, type);
}


QPixmap WindowSkin::createWindowBack(int w, int h, GSWindowStyle type){
    return WindowSkin::createWindowBack(w, h, 64, type);
}

QPixmap WindowSkin::createWindowBack(int w, int h, int base, GSWindowStyle type){
    QPixmap result(w, h);
    result.fill(Qt::transparent);
    QPixmap backFile = WindowSkin::getStyleFile(type);
    QPainter p;
    p.begin(&result);
    p.drawPixmap(0,0, w, h, backFile, 0,0, base, base);
    p.end();
    return result;
}

QPixmap WindowSkin::getStyleFile(GSWindowStyle type){
    QPixmap p;
    switch(type){
    case FFStyle:
        p = QPixmap(":/windowSkin/images/window/wskin_n.png");
        break;
    case TouhouStyle:
        p = QPixmap(":/windowSkin/images/window/windowSkin3.png");
        break;
    case TouhouStyle2:
        p = QPixmap(":/windowSkin/images/window/windowSkin4.png");
        break;
    case TestStyle:
        p = QPixmap(":/windowSkin/images/window/wskin_w.png");
        break;
    case SimpleStyle:
        p = QPixmap(":/windowSkin/images/window/windowSkin1.png");
        break;
    case PinStyle:
        p = QPixmap(":/windowSkin/images/window/Window.png");
        break;
    default:
        break;
    }
    return p;
}
